package Clothing.serviceImpl.lease;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import Clothing.bean.request.lease.AddLeaseRequest;
import Clothing.bean.request.lease.EditLeaseByIdRequest;
import Clothing.bean.request.lease.GetAllLeaseRequest;
import Clothing.bean.request.lease.GetLeaseByIdRequest;
import Clothing.bean.respose.lease.GetAllLeaseResponse;
import Clothing.bean.respose.lease.GetAllLeaseResponse.LeaseInfo;
import Clothing.bean.respose.lease.GetLeaseByIdResponse;
import Clothing.dao.AgreementDao;
import Clothing.dao.ClothingDao;
import Clothing.dao.LeaseDao;
import Clothing.dao.UserDao;
import Clothing.entity.AgreementEntity;
import Clothing.entity.ClothingEntity;
import Clothing.entity.LeaseEntity;
import Clothing.entity.LeaseForAllEntity;
import Clothing.entity.UserEntity;
import Clothing.service.lease.LeaseService;
import Clothing.util.Utils;

@Service("leaseService")
public class LeaseServiceImpl implements LeaseService {

    @Autowired
    private ClothingDao clothingDao;

    @Autowired
    private UserDao userdao;
    
    @Autowired
    private LeaseDao    leaseDao;
    
    
    @Autowired
    private AgreementDao agreementDao;

    public int getAllLease(GetAllLeaseRequest request, GetAllLeaseResponse response) {
        Map<String, Object> inputMap = new HashMap<String, Object>();
        Map<String, Object> map = new HashMap<String, Object>();
        List<Integer> clothingIdList = new ArrayList<Integer>();
        inputMap.put("clothingName", request.getClothingName());
        inputMap.put("clothingType", request.getClothingType());

        List<ClothingEntity> clothingList = clothingDao.getClothingList(inputMap);
        for (ClothingEntity clothingEntity : clothingList) {
            Integer clothingId = clothingEntity.getClothingId();
            clothingIdList.add(clothingId);
        }

        List<LeaseForAllEntity> leaseEntities = new ArrayList<LeaseForAllEntity>();
        if(clothingIdList.size()==0){
            clothingIdList = null;
        }
        map.put("clothingName", request.getClothingName());
        map.put("clothingType", request.getClothingType());
        map.put("clothingIdList", clothingIdList);
//        if(!StringUtils.isBlank(request.getReturnTime())){
//            map.put("date", Utils.convertToDate(request.getReturnTime()));
//        }
        map.put("returnTime", request.getReturnTime());
        map.put("leaseTime", request.getLeaseTime());
        map.put("status", "1");//已出租
        leaseEntities = leaseDao.getAll(map);

        List<LeaseInfo> leaseInfoList = new ArrayList<LeaseInfo>();
        for(LeaseForAllEntity leaseEntity : leaseEntities){
            LeaseInfo leaseInfo = new LeaseInfo();
            BeanUtils.copyProperties(leaseEntity, leaseInfo);
            leaseInfo.setLeaseTime(Utils.dateToStr(leaseEntity.getLeaseTime()));
            leaseInfo.setReturnTime(Utils.dateToStr(leaseEntity.getReturnTime()));
            leaseInfoList.add(leaseInfo);
        }
        response.setLeaseInfoList(leaseInfoList);
        return 0;
    }

    public int getLeaseById(GetLeaseByIdRequest request, GetLeaseByIdResponse response) {
        if (StringUtils.isBlank(request.getLeaseId())) {
            return 1;
        }
        LeaseEntity leaseEntity = leaseDao.selectByPrimaryKey(Integer.parseInt(request.getLeaseId()));
        BeanUtils.copyProperties(leaseEntity, response);
        return 0;
    }

    public int editLeaseById(EditLeaseByIdRequest request) {
        LeaseForAllEntity leaseEntity = new LeaseForAllEntity();
        BeanUtils.copyProperties(request, leaseEntity);
        leaseEntity.setLeaseId(Integer.parseInt(request.getLeaseId()));
        leaseEntity.setUserId(Integer.parseInt(request.getUserId()));
        leaseEntity.setClothingId(Integer.parseInt(request.getClothingId()));
        return leaseDao.updateByPrimaryKeySelective(leaseEntity);
    }

    public int deleteLeaseById(GetLeaseByIdRequest request) {
        if(StringUtils.isBlank(request.getLeaseId())){
            return 1;
        }
        return leaseDao.deleteByPrimaryKey(Integer.parseInt(request.getLeaseId()));
    }

    public int addLease(AddLeaseRequest request) {
        Map<String, Object> inputClothingMap = new HashMap<String, Object>();
        Map<String, Object> inputUserMap = new HashMap<String, Object>();
        AgreementEntity agreementEntity = new AgreementEntity();
        
        int leaseCount = request.getLeaseCount();
        inputUserMap.put("userId", request.getUserId());
        inputUserMap.put("userName", request.getUserName());
        inputUserMap.put("idCard", request.getIdCard());
        if(StringUtils.isBlank(request.getUserName())||
                StringUtils.isBlank(request.getIdCard())||
                StringUtils.isBlank(String.valueOf(request.getLeaseCount()))){
            return 1;
        }
        UserEntity userEntity = userdao.selectUser(inputUserMap);

        inputClothingMap.put("clothingName", request.getClothingName());
        inputClothingMap.put("clothingType", request.getClothingType());

        List<ClothingEntity> clothingList = clothingDao.getClothingList(inputClothingMap);
        if (clothingList.size() == 0 || userEntity == null) {
            return 1;
        }
        if(clothingList.get(0).getStock() < leaseCount){
            return 1;
        }
        LeaseEntity leaseEntity = new LeaseEntity();
        BeanUtils.copyProperties(request, leaseEntity);
        leaseEntity.setLeaseTime(Utils.convertToDate(request.getLeaseTime()));
        leaseEntity.setReturnTime(Utils.convertToDate(request.getReturnTime()));
        leaseEntity.setStatus("1");
        leaseEntity.setUserId(userEntity.getUserId());
        leaseEntity.setClothingId(clothingList.get(0).getClothingId());
        leaseEntity.setClothingType(clothingList.get(0).getClothingType());
        clothingList.get(0).setStock(clothingList.get(0).getStock() - leaseCount);
        clothingDao.updateByPrimaryKeySelective(clothingList.get(0));//更新库存
        
        leaseDao.insertSelective(leaseEntity);//新增租赁记录
        LeaseEntity leaseEntity2 = leaseDao.selectByParams(leaseEntity);
        
        agreementEntity.setClothingId(clothingList.get(0).getClothingId());
        agreementEntity.setCount(leaseCount);
        agreementEntity.setUserId(userEntity.getUserId());
        agreementEntity.setLeaseId(leaseEntity2.getLeaseId());
        
        //新增合同记录
        return agreementDao.insertSelective(agreementEntity);
    }

}
